package com.gitee.wsl.struct.grammar.parser

import com.gitee.wsl.struct.grammar.Grammar
import com.gitee.wsl.struct.grammar.parser.tokenizer.Tokenizer


/**
 * Lexer and parser that, thanks to `better-parse`'s context-free [Grammar], can parse a string into a structured object.
 * The term `Walker` refers to the fact this might be invoked by Quarkdown's lexer to walk through the source string,
 * in case content cannot be tokenized via regular expressions.
 * @param T the type of the parsed object
 * @param source the content to be parsed
 * @param grammar the grammar that defines the parsing rules
 * @see WalkerParsingResult
 */
//open class WalkerParser<T>(
//    private val source: CharSequence,
//    private val grammar: Grammar<T>
//) {
//    /**
//     * Parses the [source] string into an output object according to the [grammar]-defined rules.
//     * The parser interrupts when it reaches the end of the source string or when it encounters a syntax error.
//     * @return the result of the parsing operation
//     */
//    fun parse(): WalkerParsingResult<T> {
//        //val tokens = tokenizer.tokenize(source.toString())
//        val parsed = grammar.parseOrThrow(source.toString())
//
//        val endIndex = tokens[parsed.nextPosition]?.offset ?: source.length
//        val remainder = source.substring(endIndex)
//        return WalkerParsingResult(parsed.value, endIndex, remainder)
//    }
//}

/**
 * The result of a [WalkerParser] parsing operation.
 * @param T the type of result, produced by the parser
 * @param value the result value, produced by the parser
 * @param endIndex the index, relative to the input string, at which the parsing operation ended
 * @param remainder the remaining content of the input string after the parsing operation
 * @see WalkerParser
 */
data class WalkerParsingResult<T>(val value: T, val endIndex: Int, val remainder: CharSequence)
